﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.InfoConfig;
using MINERP.BLL.InfoConfig;

namespace MINERP.InfoConfig
{
    public partial class FrmCustomerPayment : FrmMDIChildFormTemplate
    {
        CustomerPaymentBLL _bll = new CustomerPaymentBLL();
        CustomerPaymentInfo_Info _customerPayInfo =null;

        private static FrmCustomerPayment _instance = null;

        private FrmCustomerPayment()
        {
            InitializeComponent();
        }

        public static FrmCustomerPayment GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmCustomerPayment();
                }
                return _instance;

            }


        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtCustomerPayName.Focus();
                return;
            }
            if (CheckData())
            {

                CustomerPaymentInfo_Info cusInfo = new CustomerPaymentInfo_Info()
                {
                    CustomerPaymentName = txtCustomerPayName.Text,
                    CustomerPaymentCode =Convert.ToInt32( txtCustomerPayCode.Text)
                };
                int ret = _bll.Add(cusInfo);
                PrepareAdd(false);
                if (ret == 0)
                {
                    LinkCustomerPayMent();
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("添加失败，请检查网路连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtCustomerPayName.Focus();
                return;
            }
            if (CheckData())
            {
                _customerPayInfo.CustomerPaymentName = txtCustomerPayName.Text;
                _customerPayInfo.CustomerPaymentCode = Convert.ToInt32(txtCustomerPayCode.Text);
                int ret = _bll.Update(_customerPayInfo);
                PrepareUpdate(false);
                if (ret == 0)
                {
                    LinkCustomerPayMent();
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("更新失败，请检查网路连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                int ret = _bll.Delete(_customerPayInfo);

                if (ret == 0)
                {
                    LinkCustomerPayMent();
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("删除失败，请检查网路连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }


        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtCustomerPayName.Text == string.Empty)
            {
                txtCustomerPayName.Focus();
                MessageBox.Show("支付方式名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtCustomerPayCode.Text == string.Empty)
            {
                txtCustomerPayCode.Focus();
                MessageBox.Show("支付方式代码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }

        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {

            txtCustomerPayName.Enabled = bl;
            txtCustomerPayCode.Enabled = bl;


            txtCustomerPayName.Text = string.Empty;
            txtCustomerPayCode.Text = string.Empty;


            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {

            txtCustomerPayName.Enabled = bl;
            txtCustomerPayCode.Enabled = bl;


            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmCustomerPayment_Load(object sender, EventArgs e)
        {
            LinkCustomerPayMent();
        }


        private void  LinkCustomerPayMent()
        {

            try
            {
                gcCustomerPay.DataSource = _bll.GetCustomerPayInfo();
                gcCustomerPay.RefreshDataSource();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtCustomerPayName.Enabled = false;
            txtCustomerPayCode.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }

        private void gvCustomerPay_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _customerPayInfo = (CustomerPaymentInfo_Info)gvCustomerPay.GetFocusedRow();
            if (_customerPayInfo != null)
            {
                txtCustomerPayCode.Text = _customerPayInfo.CustomerPaymentCode.ToString();
                txtCustomerPayName.Text = _customerPayInfo.CustomerPaymentName;
            }

        }

        private void FrmCustomerPayment_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }

      

  
    }
}
